#!/bin/bash

# Test membership, add one node after killing existing one node

. ./common

for i in 0 1; do
    _start_sheep $i
done

_wait_for_sheep 2
# start Sheepdog with two nodes
_cluster_format -c 2

for i in 2 3 4; do
    # add one node after killing existing one node
    _wait_for_sheep 2 $((i - 2))
    _kill_sheep $((i - 2))
    _wait_for_sheep 1 $((i - 1))

    _start_sheep $i
done

# kill all existing nodes
nr=2
for i in 3 4; do
    _wait_for_sheep $nr $i
    _kill_sheep $i
    nr=$(($nr - 1))
done

for i in 0 1 2 3 4; do
    _start_sheep $i
    nr=$(($nr + 1))
    _wait_for_sheep $nr 0
done

_wait_for_sheep 5
echo check whether all nodes have the same cluster info
for i in 0 1 2 3 4; do
    $DOG cluster info -p 700$i | _filter_cluster_info
done
